home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / old-stream / ostream.h < prev    next >
C/C++ Source or Header  |  1992-01-17  |  6KB  |  251 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1989, 1992 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. /* *** Version 1.2 -- nearly 100% AT&T 1.2 compatible *** */
  20.  
  21. /* ostream.h now  separately includable */
  22.  
  23. #ifndef _ostream_h
  24. #ifdef __GNUG__
  25. #pragma interface
  26. #endif
  27. #define _ostream_h 1
  28.  
  29. /* uncomment the next line to disable    ostream << char */
  30. //#define NO_OUTPUT_CHAR
  31.  
  32.  
  33. #include <File.h>
  34. #include <streambuf.h>
  35. #include <filebuf.h>
  36. #include <Filebuf.h>
  37.  
  38. class istream;
  39.  
  40. class ostream
  41. {
  42.   friend class istream;
  43. protected:
  44.   streambuf*    bp;
  45.   state_value   state;           // _good/_eof/_fail/_bad
  46.   char          ownbuf;          // true if we own *bp
  47.   
  48. public:
  49.                 ostream(const char* filename, io_mode m, access_mode a);
  50.                 ostream(const char* filename, const char* m);
  51.                 ostream(int filedesc, io_mode m);
  52.                 ostream(FILE* fileptr);
  53.                 ostream(int sz, char* buf);
  54.                 ostream(int filedesc, char* buf, int buflen);
  55.                 ostream(int filedesc);
  56.                 ostream(streambuf* s);
  57.  
  58.                ~ostream();
  59.  
  60.   ostream&      open(const char* filename, io_mode m, access_mode a);
  61.   ostream&      open(const char* filename, const char* m);
  62.   ostream&      open(int  filedesc, io_mode m);
  63.   ostream&      open(FILE* fileptr);
  64.   ostream&      open(const char* filenam, open_mode m);
  65.  
  66.   ostream&      close();
  67.   ostream&      flush();
  68.  
  69. // stream status
  70.  
  71.   int           rdstate();
  72.   int           eof();
  73.   int           fail();
  74.   int           bad();
  75.   int           good();
  76.  
  77. // other status queries
  78.  
  79.   int           readable();
  80.   int           writable();
  81.   int           is_open();
  82.  
  83.                 operator void*();
  84.   int           operator !();
  85.  
  86.   const char*   name();
  87.  
  88.   char*         bufptr();
  89.  
  90. // error handling
  91.  
  92.   void          error();
  93.   void          clear(state_value f = _good); // poorly named
  94.   void          set(state_value f); // set corresponding bit
  95.   void          unset(state_value); // clear corresponding bit
  96.   ostream&      failif(int cond);
  97.  
  98. // unformatted IO
  99.  
  100.   ostream&      put(char  c);
  101.   ostream&      put(const char* s);
  102.   ostream&      put(const char* s, int slen);
  103.            
  104. // formatted IO
  105.  
  106.   ostream&      form(const char* fmt, ...);           
  107.  
  108.   ostream&      operator << (short  n);
  109.   ostream&      operator << (unsigned short n);
  110.   ostream&      operator << (int    n);
  111.   ostream&      operator << (unsigned int n);
  112.   ostream&      operator << (long   n);
  113.   ostream&      operator << (unsigned long n);
  114. #ifdef __GNUG__
  115.   ostream&      operator << (long long n);
  116.   ostream&      operator << (unsigned long long n);
  117. #endif /* __GNUG__ */
  118.   ostream&      operator << (float  n);
  119.   ostream&      operator << (double n);
  120.   ostream&      operator << (const char* s);
  121.   ostream&      operator << (const void* ptr);
  122.  
  123. #ifndef NO_OUTPUT_CHAR
  124.   ostream&      operator << (char   c);
  125. #endif
  126.  
  127. };
  128.  
  129. extern ostream  cout;            // stdout
  130. extern ostream  cerr;            // stderr
  131.  
  132. #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
  133.  
  134.  
  135. inline void ostream::clear(state_value flag)
  136. {
  137.   state = flag;
  138. }
  139.  
  140. inline void ostream::set(state_value flag)
  141. {
  142.   state = state_value(int(state) | int(flag));
  143. }
  144.  
  145. inline void ostream::unset(state_value flag)
  146. {
  147.   state = state_value(int(state) & ~int(flag));
  148. }
  149.  
  150. inline int ostream::rdstate()
  151. {
  152.   return int(state);
  153. }
  154.  
  155. inline int ostream::good()
  156. {
  157.   return state == _good;
  158. }
  159.  
  160. inline int ostream::eof()
  161. {
  162.   return int(state) & int(_eof);
  163. }
  164.  
  165. inline int ostream::fail()
  166. {
  167.   return int(state) & int(_fail);
  168. }
  169.  
  170. inline int ostream::bad()
  171. {
  172.   return int(state) & int(_bad);
  173. }
  174.  
  175. inline ostream::operator void*()
  176. {
  177.   return (state == _good)? this : 0;
  178. }
  179.  
  180. inline int ostream::operator !()
  181. {
  182.   return (state != _good);
  183. }
  184.  
  185. inline ostream& ostream::failif(int cond)
  186. {
  187.   if (cond) set(_fail); return *this;
  188. }
  189.  
  190. inline int ostream::is_open()
  191. {
  192.   return bp->is_open();
  193. }
  194.  
  195. inline int ostream::readable()
  196. {
  197.   return 0;
  198. }
  199.  
  200. inline int ostream::writable()
  201. {
  202.   return (bp != 0) && (state == _good);
  203. }
  204.  
  205.  
  206. inline char* ostream::bufptr()
  207. {
  208.   return bp->base;
  209. }
  210.  
  211. inline ostream& ostream::flush()
  212. {
  213.   bp->overflow(); return *this;
  214. }
  215.  
  216. inline ostream& ostream::close()
  217. {
  218.   bp->overflow(); bp->close();  return *this;
  219. }
  220.  
  221. inline ostream& ostream::put(char ch)
  222. {
  223.   return failif((state != _good) || bp->sputc((int)ch &0xff) == EOF);
  224. }
  225.  
  226. #ifndef NO_OUTPUT_CHAR
  227. inline ostream& ostream::operator << (char ch)
  228. {
  229.   return failif((state != _good) || bp->sputc((int)ch &0xff) == EOF);
  230. }
  231. #endif
  232.  
  233. inline ostream& ostream::put(const char* s)
  234. {
  235.   return failif((state != _good) || bp->sputs(s) == EOF);
  236. }
  237.  
  238. inline ostream& ostream::put(const char* s, int len)
  239. {
  240.   return failif((state != _good) || bp->sputsn(s, len) == EOF);
  241. }
  242.  
  243. inline ostream& ostream::operator << (const char* s)
  244. {
  245.   return failif((state != _good) || bp->sputs(s) == EOF);
  246. }
  247.  
  248. #endif
  249.  
  250. #endif
  251.